package com.tsd.common.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.common.entity.CommonSnValue;
import com.tsd.common.entity.CommonSnValueExt;
import com.tsd.common.service.CommonSnValueService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.entity.SysUserExt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：CommonSnValue序号表,为各种编号提供最新值记录 控制层
 *
 * @author huawei
 * @date 2020/07/29 17:08:20
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "common_sn_value-序号表,为各种编号提供最新值记录", description = "序号表,为各种编号提供最新值记录的API描述")
public class CommonSnValueController extends BaseController {

    @Resource
    private CommonSnValueService commonSnValueService;

    @ApiOperation(value = "根据id获取序号表,为各种编号提供最新值记录", notes = "根据id获取序号表,为各种编号提供最新值记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "序号表,为各种编号提供最新值记录id", required = true, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getCommonSnValueById.action", method = RequestMethod.POST)
    public HttpResult<CommonSnValue> getCommonSnValueById() {
        HttpResult<CommonSnValue> hr = new HttpResult<CommonSnValue>();
        try {
            SysUserExt opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            CommonSnValue commonSnValue = commonSnValueService.getCommonSnValueById(id);
            hr.setData(commonSnValue);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id获取序号表,为各种编号提供最新值记录", notes = "根据id获取序号表,为各种编号提供最新值记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "序号表,为各种编号提供最新值记录id", required = true, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getCommonSnValueByCode.action", method = RequestMethod.POST)
    public HttpResult<Object> getCommonSnValueByCode() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            String code = HlpUtils.getString(map, "code");
            super.checkEmpty(code, "请提供参数：code");
            CommonSnValueExt valueExt = commonSnValueService.getCommonSnValueByCode(code);
            hr.setData(valueExt);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除序号表,为各种编号提供最新值记录", notes = "根据id删除序号表,为各种编号提供最新值记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "序号表,为各种编号提供最新值记录id", required = true, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteCommonSnValue.action", method = RequestMethod.POST)
    public HttpResult<String> deleteCommonSnValue() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUserExt opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            commonSnValueService.deleteCommonSnValue(id, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存序号表,为各种编号提供最新值记录", notes = "保存序号表,为各种编号提供最新值记录，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "序号表,为各种编号提供最新值记录", required = true, paramType = "body", dataType = "CommonSnValue")
    })
    @ResponseBody
    @RequestMapping(value = "/saveCommonSnValue.action", method = RequestMethod.POST)
    public HttpResult<String> saveCommonSnValue() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUserExt opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map);
            CommonSnValue t = JSON.parseObject(json, CommonSnValue.class);
            commonSnValueService.saveCommonSnValue(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询序号表,为各种编号提供最新值记录", notes = "分页查询序号表,为各种编号提供最新值记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findCommonSnValues.action", method = RequestMethod.POST)
    public HttpResult<List<CommonSnValueExt>> findCommonSnValues() {
        HttpResult<List<CommonSnValueExt>> hr = new HttpResult<List<CommonSnValueExt>>();
        try {
            SysUserExt opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>();
            Integer dr = HlpUtils.getInteger(map, "dr", 0);
            params.put("dr", dr);
            String search_value = HlpUtils.getString(map, "search_value");
            //super.checkEmpty(search_value, "请提供参数search_value");
            if (!HlpUtils.isEmpty(search_value)) {
                params.put("search_value", "%" + search_value + "%");
            }
            PageBean page = new PageBean(map);
            List<CommonSnValueExt> list = commonSnValueService.findCommonSnValues(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
